/**
*   \file   aabbtopoint.h
*   \author Dave Reid
*   \brief  Header file for finding the closest point on an aabb to another point.
*/
#ifndef __EAGL_CLOSESTPOINT_AABBTOPOINT_H_
#define __EAGL_CLOSESTPOINT_AABBTOPOINT_H_

#include "../types.h"

namespace eagl
{

inline vec3 closestpoint_aabbtopoint(const vec3 &extents, const vec3 &position, const vec3 &point)
{
    vec3 p;

    for (int i = 0; i < 3; ++i)
    {
        float v = point[i];

        float aabb_min = position[i] - extents[i];
        float aabb_max = position[i] + extents[i];

        if (v < aabb_min)
        {
            v = aabb_min;
        }

        if (v > aabb_max)
        {
            v = aabb_max;
        }

        p[i] = v;
    }

    return p;
}

}

#endif  // __EAGL_CLOSESTPOINT_AABBTOPOINT_H_